import {defHttp} from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";
import { BasicTable } from '/@/components/Table';

const { createConfirm } = useMessage();

enum Api {
  list = '/projects/projects/list',
  save='/projects/projects/add',
  edit='/projects/projects/edit',
  deleteOne = '/projects/projects/delete',
  deleteBatch = '/projects/projects/deleteBatch',
  importExcel = '/projects/projects/importExcel',
  exportXls = '/projects/projects/exportXls',
  queryDataById = '/projects/projects/queryById',
  subProjectsList = '/projects/projects/querySubProjectsByMainId',
  deleteSubProjectsBatch = '/projects/projects/subprojects/deleteBatch',
  createTeam = '/projects/projects/createTeam',
  createSubProjectTeam = '/projects/projects/createSubProjectTeam',
  queryTeamMembers = '/teams/teams/queryTeamMembers',
  addTeamMember = '/teams/teams/addTeamMember',
  updateSubProject = '/projects/projects/updatesubprojects',
  getApprovedContracts = '/projects/projects/getApprovedContracts',
  updateProjectStatus = '/projects/projects/updateStatus',
  getProjectRemainingAmount = '/projects/projects/getProjectRemainingAmount',
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

export const createTeam = (params) => defHttp.post({ url: Api.createTeam, params });

export const createSubProjectTeam = (params) => defHttp.post({ url: Api.createSubProjectTeam, params });

// 更新项目状态
export const updateProjectStatus = (params) => defHttp.post({ url: Api.updateProjectStatus, params });
export const queryTeamMembers = (params) => defHttp.get({ url: Api.queryTeamMembers, params });
export const addTeamMember = (params) => defHttp.post({ url: Api.addTeamMember, params });
export const getApprovedContracts = (params) => defHttp.get({ url: Api.getApprovedContracts, params });

// 获取项目剩余金额
export const getProjectRemainingAmount = (projectId) => {
  return defHttp.get(
    { url: Api.getProjectRemainingAmount, params: { projectId } },
    { successMessageMode: 'none' }
  );
};


/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 查询子表数据
 * @param params
 */
export const querySubProjectsListByMainId = (id) => defHttp.get({url: Api.subProjectsList, params:{ id }});

/**
 * 列表接口
 * @param params
 */
export const list = (params) =>
  defHttp.get({url: Api.list, params}).then(res => {
    // 兼容不同返回结构
    if (Array.isArray(res)) {
      return { records: res, total: res.length }
    }
    if (res && res.result) {
      // 兼容 result 为数组或对象
      if (Array.isArray(res.result)) {
        return { records: res.result, total: res.result.length }
      }
      return res.result
    }
    if (res && res.data) {
      // 兼容 data 为数组或对象
      if (Array.isArray(res.data)) {
        return { records: res.data, total: res.data.length }
      }
      return res.data
    }
    return res
  });

/**
 * 删除单个
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({url: Api.deleteOne, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}
/**
 * 批量删除子项目
 * @param params
 */
export const batchDeleteSubProjects = (params, handleSuccess) => {
  return defHttp.delete(
    { url: Api.deleteSubProjectsBatch, params },
    { joinParamsToUrl: true, successMessageMode: 'none' }
  ).then(() => {
    handleSuccess();
  });
};
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  console.log(`==================== ${isUpdate ? '更新' : '新增'}项目 API 调用 ====================`);
  console.log('请求URL:', url);
  console.log('请求参数:', JSON.stringify(params, null, 2));
  console.log('合同金额:', params.contractAmount);
  console.log('剩余金额:', params.remainedAmount);
  console.log('=================================================================');
  return defHttp.post({url: url, params});
}

/**
* 根据id查询数据
* @param params
*/
export const queryDataById = (id) => defHttp.get({url: Api.queryDataById, params:{ id }});

// 新增子项目
export const addSubProject = (params) => {
  return defHttp.post(
    { url: '/projects/projects/addSubProject', params },
    { successMessageMode: 'none' }
  );
};

// 修改子项目
export const updateSubProject = (params) => {
  return defHttp.post(
    { url: Api.updateSubProject, params },
    { successMessageMode: 'none' }
  );
};

/**
 * 查询所有子项目
 * @param params
 */
export const findAllSubProjectsById = (params) => {
  return defHttp.get({ url: '/projects/projects/subProjects/findAllById', params }).then(res => {
    return res; // 保持原有返回
  });
}

